package com.colud.client.controller;

import com.colud.client.JT809.*;
import com.colud.client.db.Dao.JT809Dao;
import com.colud.client.db.Dto.JT8090x1202Dto;
import com.colud.client.test.JT809BodyManager;
import com.colud.client.test.JT809Manager;
import com.colud.client.util.JT809Constants;
import com.colud.client.vo.JT8090x1201Entity;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author mfy
 * @date 2021/10/15
 */
@RestController
@RequestMapping("/send")
public class JT809SendMsgController {

    private Logger log = LoggerFactory.getLogger(JT809SendMsgController.class);

    @Autowired
    private JT809Dao jt809Dao;

    @Autowired
    private JT809Manager jt809Manager;

    @GetMapping("/login")
    @ApiOperation("主链路登录请求消息")
    public Boolean sendLoginMsg() {

        //登录
        boolean login2Gov = jt809Manager.login2Gov();
        return login2Gov;
    }

    @GetMapping("/disconnect")
    @ApiOperation("主链路注销请求消息")
    public void sendT1003Msg() {

        //主链路注销
        T1003 t1003 = new T1003();
        t1003.setUserId(700013);
        t1003.setPassword("27839781");
        jt809Manager.sendMsg(JT809BodyManager.T1003DataBody(t1003));
    }

    @GetMapping("/t1201")
    @ApiOperation("实时上传车辆注册信息")
    public JT8090x1201Entity sendT1201Msg(@RequestParam("mobile_no") String mobileNo) {
        T1201 awsVo = new T1201();
        JT8090x1201Entity x1201Entity = jt809Dao.queryDeviceMsg(mobileNo);
        awsVo.setVehicleNo(x1201Entity.getPlateNo());
        /**
         * 制造商
         */
        awsVo.setProducerId(x1201Entity.getMakerId());
        awsVo.setTerminalId(x1201Entity.getDeviceModel());
        awsVo.setTerminalModelType(String.valueOf(x1201Entity.getDeviceId()));
        awsVo.setTerminalSimcode(x1201Entity.getMobileNo());
        //发送定位信息
        jt809Manager.sendMsg(JT809BodyManager.T1201DataBody(awsVo));
        return x1201Entity;
    }

    @GetMapping("/t1202s")
    @ApiOperation("实时上传时间段车辆定位信息")
    public List<JT8090x1202Dto> sendT1202MsgByTime(@RequestParam("mobile_no") String mobileNo,
                                                   @RequestParam("start_time") String start,
                                                   @RequestParam("end_time") String end) {
        T1202 awsVo = new T1202();
        List<JT8090x1202Dto> x1202DtoList = jt809Dao.queryLocationMsg(mobileNo, start, end);
        for (JT8090x1202Dto x1202Dto : x1202DtoList) {
            log.info("发送定位信息！");
            awsVo.setVehicleNo(x1202Dto.getPlateNo());
            awsVo.setState(x1202Dto.getStatus());
            awsVo.setDirection(x1202Dto.getDirection());
            awsVo.setLon(String.valueOf(x1202Dto.getLongitude()));
            awsVo.setLat(String.valueOf(x1202Dto.getLatitude()));
            awsVo.setAltitude(x1202Dto.getAltitude());
            awsVo.setVec1(x1202Dto.getSpeed());
            awsVo.setAlarm(x1202Dto.getWarningMark());
            //发送定位信息
            jt809Manager.sendMsg(JT809BodyManager.T1202DataBody(awsVo));
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return x1202DtoList;
    }

    @GetMapping("/t1202")
    @ApiOperation("实时上传车辆定位信息")
    public JT8090x1202Dto sendT1202Msg(@RequestParam("mobile_no") String mobileNo) {
        T1202 awsVo = new T1202();
        JT8090x1202Dto x1202Dto = jt809Dao.queryOneT1202Msg(mobileNo);
        awsVo.setVehicleNo(x1202Dto.getPlateNo());
        awsVo.setState(x1202Dto.getStatus());
        awsVo.setDirection(x1202Dto.getDirection());
        awsVo.setLon(String.valueOf(x1202Dto.getLongitude()));
        awsVo.setLat(String.valueOf(x1202Dto.getLatitude()));
        awsVo.setAltitude(x1202Dto.getAltitude());
        awsVo.setVec1(x1202Dto.getSpeed());
        awsVo.setAlarm(x1202Dto.getWarningMark());
        //发送定位信息
        jt809Manager.sendMsg(JT809BodyManager.T1202DataBody(awsVo));
        return x1202Dto;
    }

    @GetMapping("/t1207")
    @ApiOperation("申请交换指定车辆定位信息请求")
    public void sendT1207Msg(@RequestParam("mobile_no") String mobileNo,
                             @RequestParam("start_time") String start,
                             @RequestParam("end_time") String end) {
        T1207 awsVo = new T1207();
        JT8090x1201Entity x1201Entity = jt809Dao.queryDeviceMsg(mobileNo);
        awsVo.setVehicleNo(x1201Entity.getPlateNo());
        awsVo.setVehicleColor((byte) 1);
        awsVo.setDataType(JT809Constants.UP_EXG_MSG_APPLY_FOR_MONITOR_STARTUP);
        awsVo.setStartTime(start);
        awsVo.setEndTime(end);
        //发送信息
        jt809Manager.sendMsg(JT809BodyManager.T1207DataBody(awsVo));
    }

    @GetMapping("/t1208")
    @ApiOperation("取消交换指定车辆定位")
    public void sendT1208Msg(@RequestParam("mobile_no") String mobileNo) {
        T1208 awsVo = new T1208();
        JT8090x1201Entity x1201Entity = jt809Dao.queryDeviceMsg(mobileNo);
        awsVo.setVehicleNo(x1201Entity.getPlateNo());
        awsVo.setVehicleColor((byte) 1);
        awsVo.setDataType(JT809Constants.UP_EXG_MSG_APPLY_FOR_MONITOR_END);
        //发送信息
        jt809Manager.sendMsg(JT809BodyManager.T1208DataBody(awsVo));
    }

    @GetMapping("/t1209")
    @ApiOperation("补发车辆定位信息请求")
    public void sendT1209Msg(@RequestParam("mobile_no") String mobileNo,
                             @RequestParam("start_time") String start,
                             @RequestParam("end_time") String end) {
        T1207 awsVo = new T1207();
        JT8090x1201Entity x1201Entity = jt809Dao.queryDeviceMsg(mobileNo);
        awsVo.setVehicleNo(x1201Entity.getPlateNo());
        awsVo.setVehicleColor((byte) 1);
        awsVo.setDataType(JT809Constants.UP_EXG_MSG_APPLY_HISGNSSDATA_REQ);
        awsVo.setStartTime(start);
        awsVo.setEndTime(end);
        //数据体

        //发送信息
        jt809Manager.sendMsg(JT809BodyManager.T1207DataBody(awsVo));
    }

    @GetMapping("/t1402")
    @ApiOperation("上报报警信息请求")
    public void sendT1402Msg(@RequestParam("mobile_no") String mobileNo) {
        T1402 awsVo = new T1402();
        JT8090x1201Entity x1201Entity = jt809Dao.queryDeviceMsg(mobileNo);
        awsVo.setVehicleNo(x1201Entity.getPlateNo());
        awsVo.setVehicleColor((byte) 1);
        awsVo.setDataType(JT809Constants.UP_WARN_MSG_ADPT_INFO);
        awsVo.setWarnSrc((byte) 1);
        awsVo.setWarnType(1);//超速报警
        awsVo.setWarnTime("2021-08-09 00:00:00");
        awsVo.setInfoId(1234);
        awsVo.setInfoLength(20);
        awsVo.setInfoContent("报警信息上传");
        //发送信息
        jt809Manager.sendMsg(JT809BodyManager.T1402DataBody(awsVo));
    }

}
